package com.qinghe.pro.controller.main.article;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.article.Article;
import com.qinghe.pro.service.article.ArticleService;
import com.qinghe.pro.service.article.ColumnService;
import com.qinghe.utils.AjaxObject;
import com.qinghe.utils.Page;

@Controller
@RequestMapping("/manager/article")
public class ArticleController {

	@Autowired
	@Qualifier("articleServiceImpl")
	private ArticleService articleService;

	@Autowired
	@Qualifier("columnServiceImpl")
	private ColumnService columnService;

	private static final String CREATE = "manager/article/create";
	private static final String UPDATE = "manager/article/update";
	private static final String VIEW = "manager/article/view";
	private static final String LIST = "manager/article/list";

	@RequiresPermissions("Article:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
        map.put("columnList", columnService.findMainAsTree());
		return CREATE;
	}

	@RequiresPermissions("Article:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody AjaxObject create(Article article) {
		articleService.insert(article);

		return AjaxObject.newOk("添加新闻成功！");
	}

	@RequiresPermissions("Article:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable Integer id, Map<String, Object> map) {
		Article article = articleService.get(id);

		map.put("article", article);
        map.put("columnList", columnService.findMainAsTree());
		return UPDATE;
	}

	@RequiresPermissions("Article:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody AjaxObject update(Article article) {
		articleService.update(article);

		return AjaxObject.newOk("修改新闻成功！");
	}

	@RequiresPermissions("Article:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody AjaxObject delete(@PathVariable Integer id) {
		articleService.delete(id);

		return AjaxObject.newOk("删除新闻成功！");
	}

	@RequiresPermissions("Article:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody AjaxObject deleteMany(@RequestParam("ids[]") List<Integer> ids) {
		for (Integer id : ids) {
			articleService.delete(id);
		}

		return AjaxObject.newOk("删除新闻成功！");
	}

	@RequiresPermissions("Article:view")
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Integer id, Map<String, Object> map) {
		Article article = articleService.get(id);
		
		map.put("article", article);
		return VIEW;
	}

	@RequiresPermissions("Article:show")
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Article article, Page page, Map<String, Object> map) {
		List<Article> articleList = articleService.findByPage(article, page);

		map.put("page", page);
		map.put("articleList", articleList);
        map.put("columnList", columnService.findMainAsTree());

		return LIST;
	}

}
